<?php

class cronjobLaunchAlertTask extends sfBaseTask {

    protected function configure() {
        // // add your own arguments here
        // $this->addArguments(array(
        //   new sfCommandArgument('my_arg', sfCommandArgument::REQUIRED, 'My argument'),
        // ));

        $this->addOptions(array(
            new sfCommandOption('application', null, sfCommandOption::PARAMETER_REQUIRED, 'The application name', 'backend'),
            new sfCommandOption('env', null, sfCommandOption::PARAMETER_REQUIRED, 'The environment', 'dev'),
            new sfCommandOption('connection', null, sfCommandOption::PARAMETER_REQUIRED, 'The connection name', 'doctrine'),
                // add your own options here
        ));

        $this->namespace = 'cronjob';
        $this->name = 'launchAlert';
        $this->briefDescription = 'generate automatically alert information from folders users';
        $this->detailedDescription = <<<EOF
The [cronjob:launchAlert|INFO] task does things.
Call it with:

  [php symfony cronjob:launchAlert|INFO]
EOF;
    }

    protected function execute($arguments = array(), $options = array()) {
        // initialize the database connection
        $databaseManager = new sfDatabaseManager($this->configuration);
        $connection = $databaseManager->getDatabase($options['connection'])->getConnection();


        $qDossier = Doctrine::getTable('Dossier')->createQuery('d')
                        ->where('d.is_close = 0')
                        ->execute();

        foreach ($qDossier as $dossier) {

            //Vérification des dossiers si l'envoi des pieces joint pour étude sont bien envoyé
            if ($dossier->getAttachmentValid() == 0) {

                $alertes_type = $dossier->getListAlertType();
                $today = new DateTime();
                $interval = date_diff($dossier->getDateTimeObject('created_at'), $today)->format('%d');

                if (count($alertes_type) == 0) {
                    if ($interval == 1) {
                        $this->createAlert($dossier, 1);
                        //TODO send email
                    } else if ($interval == 7) {
                        $this->createAlert($dossier, 2);
                        //TODO send email
                    }
                } elseif (!in_array(1, $alertes_type) && $interval == 1) {
                    $this->createAlert($dossier, 1);
                    //TODO send email
                } elseif (!in_array(2, $alertes_type) && $interval == 7) {
                    $this->createAlert($dossier, 2);
                    //TODO send email
                }
            }
        }
    }

    private function createAlert($dossier, $type = 0) {
        $newAlert = new Alert();
        $newAlert->setDossier($dossier);
        $newAlert->setCustomer($dossier->getCustomer());
        $newAlert->setTypeAlert($type);
        $newAlert->setSubject(AlertTable::$type_alert[$type]);
        $newAlert->setIsValid(true);
        $newAlert->save();
    }

}
